package com.jackrain.nea.ext.oss;


import lombok.Getter;

import java.util.List;
import java.util.Map;

/**
 * OSS操作的返回值
 * @author blqw
 * @date 2017-12
 */

public class OssResult {
    /**
     * 是否有异常
     */
    @Getter
    private final boolean error;
    /**
     * 操作中的异常对象
     */
    @Getter
    private final Exception exception;
    /**
     * 终结点
     */
    @Getter
    private final String endpoint;
    /**
     * 存储域名
     */
    @Getter
    private final String domain;
    /**
     * 存储空间
     */
    @Getter
    private final String bucket;
    /**
     * 对象标识(可以理解为相对路径)
     */
    @Getter
    private final String key;
    /**
     * 操作标识
     */
    @Getter
    private final String eTag;
    /**
     * 额外数据
     */
    @Getter
    private final Object data;

    @Getter
    private final OssObject ossObject;

    /**
     * 初始化操作
     *
     * @param endpoint  终结点
     * @param domain    存储域名
     * @param bucket    存储空间
     * @param key       对象标识
     * @param eTag      操作标识
     * @param exception 操作异常
     * @param data      额外数据
     */
    public OssResult(String endpoint, String domain, String bucket, String key, String eTag, Exception exception, Object data) {
        this.exception = exception;
        this.endpoint = endpoint;
        this.domain = domain;
        this.bucket = bucket;
        this.key = key;
        this.eTag = eTag;
        this.error = exception != null;
        this.data = data;
        this.ossObject = new OssObject(bucket, domain, key, (Map<String, String>)null);
    }

    /**
     * 初始化操作
     *
     * @param endpoint  终结点
     * @param eTag      操作标识
     * @param exception 操作异常
     * @param data      额外数据
     * @param ossObject Oss存储对象
     */
    public OssResult(String endpoint, String eTag, Exception exception, Object data, OssObject ossObject) {
        this.exception = exception;
        this.endpoint = endpoint;
        this.eTag = eTag;
        this.error = exception != null;
        this.data = data;
        this.ossObject = ossObject;
        if (ossObject != null) {
            this.domain = ossObject.getDomain();
            this.bucket = ossObject.getBucket();
            this.key = ossObject.getKey();
        } else {
            this.domain = null;
            this.bucket = null;
            this.key = null;
        }
    }

    /**
     * 完整路径
     *
     * @return
     */
    public String getFullUrl() {
        return domain.endsWith("/") ? domain + key : domain + "/" + key;
    }
}

